package com.bs.service;

import com.bs.domain.Partitions;
import com.bs.service.callback.ModuleNameCallBack;
import com.bs.utils.DataTable;
import com.bs.utils.EiInfo;
import com.bs.utils.iPlat4CHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by wujian
 * Date: 2017-12-13
 * Time: 15:22
 */
@Service
public class ModuleNameService {

    private final Logger log = LoggerFactory.getLogger(ModuleNameService.class);

    @Autowired
    private PartitionsService partitionsService;

    public void getModule(String partition, String sql, String param, ModuleNameCallBack moduleNameCallBack) {
        Partitions partitions = new Partitions();
        partitions.setEname(partition);
        List<Partitions> partitionsList = partitionsService.getPartitionss(partitions);
        String baseUrl = partitionsList.get(0).getUrl();


        try {
            EiInfo info = new EiInfo();
            info.SysInfo.put("SvcName", "eped_dyn_sql");
            info.SysInfo.put("Sender", "admin");
            info.SysInfo.put("UUID", "");
            info.SysInfo.put("UserName", "");
            info.SysInfo.put("CompanyCode", "");
            info.SysInfo.put("CompanyName", "");
            info.SysInfo.put("Msg", "");
            info.SysInfo.put("Flag", 0);
            info.SysInfo.put("ForeMachine", "");

            info.table1Columns.put(0, new JSONObject("{'Name':'SQL'}"));
            String sql1 = String.format(sql, param);
            info.table1Rows.put(0, new JSONArray().put(sql1));
            String resp = iPlat4CHttpClient.post(baseUrl, info.toString());

            if (resp != null) {
                EiInfo outBlock = new EiInfo(resp);
                if (outBlock.SysInfo.has("flag")) {
                    log.error(outBlock.SysInfo.getString("msg"));
                }
                DataTable table = outBlock.GetTable(0);

                moduleNameCallBack.onSuccess(table);


            }
        } catch (Exception ex) {
            ex.getStackTrace();
            log.error(ex.toString());
            moduleNameCallBack.onFailure(ex.toString());
        }
    }

    public DataTable getModule(String partition, String sql, String param) {
        Partitions partitions = new Partitions();
        partitions.setEname(partition);
        List<Partitions> partitionsList = partitionsService.getPartitionss(partitions);
        String baseUrl = partitionsList.get(0).getUrl();


        try {
            EiInfo info = new EiInfo();
            info.SysInfo.put("SvcName", "eped_dyn_sql");
            info.SysInfo.put("Sender", "admin");
            info.SysInfo.put("UUID", "");
            info.SysInfo.put("UserName", "");
            info.SysInfo.put("CompanyCode", "");
            info.SysInfo.put("CompanyName", "");
            info.SysInfo.put("Msg", "");
            info.SysInfo.put("Flag", 0);
            info.SysInfo.put("ForeMachine", "");

            info.table1Columns.put(0, new JSONObject("{'Name':'SQL'}"));
            String sql1 = String.format(sql, param);
            info.table1Rows.put(0, new JSONArray().put(sql1));
            String resp = iPlat4CHttpClient.post(baseUrl, info.toString());

            if (resp != null) {
                EiInfo outBlock = new EiInfo(resp);
                if (outBlock.SysInfo.has("flag")) {
                    log.error(outBlock.SysInfo.getString("msg"));
                }
                DataTable table = outBlock.GetTable(0);

                return table;


            }
        } catch (Exception ex) {
            ex.getStackTrace();
            log.error(ex.toString());
        }
        return null;
    }
}
